package com.dangdi.show.service;

import com.aliyuncs.exceptions.ClientException;
import com.dangdi.show.dao.SmsDemo;
import com.dangdi.show.dao.WechatUserDao;
import com.gexin.fastjson.JSONArray;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class WechatUserService {
    @Resource
    WechatUserDao wechatUserDao;

    //查找openid
    public Map<String, Object> FindOpenIdByOpenId(String open_id) {
        return wechatUserDao.FindOpenIdByOpenId(open_id);
    }

    //插入openid
    public void InserOpenId(String openID) {
        wechatUserDao.InserOpenId(openID);
    }

    //添加地址
    public void AddWecharUserAddress(String user_name, String user_tel, String user_sheng, String user_shi, String user_qu, String user_add, String open_id) {
        wechatUserDao.AddWecharUserAddress(user_name, user_tel, user_sheng, user_shi, user_qu, user_add, open_id);
    }

    //获取地址列表
    public List<Map<String, Object>> getAddressListByOpenId(String openid) {
        return wechatUserDao.getAddressListByOpenId(openid);
    }

    //获取地址
    public Map<String, Object> getAddressByAddressId(int address_id) {
        return wechatUserDao.getAddressByAddressId(address_id);
    }

    //删除地址
    public int del_address(int address_id) {
        return wechatUserDao.del_address(address_id);
    }

    //修改地址
    public int updateAddress(String user_name, String user_tel, String user_sheng, String user_shi, String user_qu, String user_add, int address_id) {
        return wechatUserDao.updateAddress(user_name, user_tel, user_sheng, user_shi, user_qu, user_add, address_id);
    }

    //获取轮播图
    public List<Map<String, Object>> getIndex_side_imgS() {
        return wechatUserDao.getIndex_side_imgS();
    }

    //获取订单列表
    public List<Map<String, Object>> getOrderList(String openid, int page) {
        System.out.println("page=" + page);
        return wechatUserDao.getOrderList(openid, page);
    }

    //获取单订单
    public List<Map<String, Object>> getOrderInfo(int order_id) {
        return wechatUserDao.getOrderInfo(order_id);
    }

    //获取演员
    public List<Map<String, Object>> getActorListByorder_id(@Param("order_id") int order_id) {
        return wechatUserDao.getActorListByorder_id(order_id);
    }

    //下单
    public Map<String, Object> xiadan(@Param("order_total_price") String order_total_price, @Param("open_id") String open_id, @Param("address_id") int address_id, @Param("show_time") Date show_time, @Param("order_number") String order_number, JSONArray feeScale_id_list) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 1);
        map.put("msg", "下单成功");
        System.out.println(show_time);
        if (wechatUserDao.getOrderByOpen_id(open_id) != null) {
            map.put("code", -2);
            map.put("msg", "你有为付款订单，请先处理");
            map.put("order_id", wechatUserDao.getOrderByOpen_id(open_id).get("order_id").toString());
            return map;
        }
        int row = wechatUserDao.xiadan(order_total_price, open_id, address_id, show_time, order_number);
        map.put("order_id", wechatUserDao.getOrderByOrder_number(order_number).get("order_id").toString());
        if (row == 1) {
            int order_id = (int) wechatUserDao.getOrderByOrder_number(order_number).get("order_id");
            for (int i = 0; i < feeScale_id_list.size(); i++) {
                row = wechatUserDao.addFeeScaleByorder_id(order_id, (int) feeScale_id_list.get(i));
                if (row != 1) {
                    row = -1;
                    map.put("code", -1);
                    map.put("msg", "订单生成成功，添加节目失败");
                    return map;
                }
            }
        }

        return map;
    }

    public int pay(@Param("order_number") String order_number) throws ClientException, InterruptedException {
        int row = wechatUserDao.pay(order_number);
        Map serviceTel = new HashMap();
        serviceTel.put("manager_tel", "13820688282");
        if (row == 1) {
            List<Map<String, Object>> managers = wechatUserDao.getNewOrderManagerList();
            for (int i = 0; i < managers.size(); i++) {
                SmsDemo.sendSms(managers.get(i).get("manager_tel").toString(), managers.get(i), SmsDemo.MSG_TO_MANAGER_NEW_ORDER);
            }
            SmsDemo.sendSms(wechatUserDao.getUser_telByOrder_number(order_number), serviceTel, SmsDemo.MSG_TO_USER_NEW_ORDER);
        }
        return row;
    }

    public int updateTransaction(@Param("transaction_id") String transaction_id, @Param("order_number") String order_number) {
        return wechatUserDao.updateTransaction(transaction_id, order_number);
    }

    public int deleteOrder(@Param("order_id") int order_id) {
        return wechatUserDao.deleteOrder(order_id);
    }

    public Map<String, Object> getRefondTime(@Param("order_number") String order_number) {
        return wechatUserDao.getRefondTime(order_number);
    }

    public int updateOrderToTuikuan(@Param("out_refund_no") String out_refund_no, @Param("order_number") String order_number) throws ClientException, InterruptedException {
        if (wechatUserDao.updateOrderToTuikuan(out_refund_no, order_number) == 1) {
            List<Map<String, Object>> managers = wechatUserDao.getNewOrderManagerList();
            Map m = new HashMap();
            m.put("order_number", order_number);
            for (int i = 0; i < managers.size(); i++) {
                SmsDemo.sendSms(managers.get(i).get("manager_tel").toString(), managers.get(i), SmsDemo.MSG_TO_MANAGER_ORDER_REFOND);
            }
            SmsDemo.sendSms(wechatUserDao.getUser_telByOrder_number(order_number), m, SmsDemo.MSG_TO_USER_ORDER_REFOND);
            return 1;
        }
        return -1;
    }

    public int UpdateOrderSales(@Param("order_grade") int order_grade, @Param("order_sales") String order_sales, @Param("order_id") int order_id) {
        return wechatUserDao.UpdateOrderSales(order_grade, order_sales, order_id);
    }
}
